package redecolaborativa.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;

public class Notificacoes extends Composite {
	private FachadaAsync fachada;
	private String idSessao;
	private String login;
	private ScrollPanel painelGeral;
	final AbsolutePanel absolutePanel_1;
	
	public Notificacoes(final FachadaAsync fachada, final ScrollPanel painelGeral, final String idSessao, final String login) {
		this.fachada = fachada;
		this.idSessao = idSessao;
		this.login = login;
		this.painelGeral = painelGeral;
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("669px", "432px");
		
		final ScrollPanel scrollPanel = new ScrollPanel();
		absolutePanel.add(scrollPanel, 20, 48);
		scrollPanel.setSize("616px", "374px");
		
		this.absolutePanel_1 = new AbsolutePanel();
		scrollPanel.setWidget(absolutePanel_1);
		absolutePanel_1.setSize("100%", "100%");
		
		final DecoratedTabBar decoratedTabBar = new DecoratedTabBar();
		decoratedTabBar.addTab("Solicitacoes de Amizade");
		decoratedTabBar.addTab("Solicitacoes de Emprestimo");
		decoratedTabBar.addTab("Requisicoes de Devolucao");
		
		decoratedTabBar.addSelectionHandler(new SelectionHandler<Integer>() {
			public void onSelection(SelectionEvent<Integer> event) {
				switch (decoratedTabBar.getSelectedTab()) {
				case 0:
					mostraSolicitacoesDeAmizade();
					break;
				case 1:
					mostraSolicitacoesDeEmprestimo();
					break;
				case 2:
					mostraRequisicoesDeDevolucao();
					break;
				}
			}
		});
		
		absolutePanel.add(decoratedTabBar, 10, 10);
		decoratedTabBar.setSize("592px", "26px");
	}
	
	private void mostraSolicitacoesDeAmizade() {
		fachada.getSolicitacoesDeAmizade(idSessao, new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> result) {
				absolutePanel_1.clear();
				if (result.isEmpty()) {
					absolutePanel_1.add(new Label("Nenhuma solicitacao de amizade!"));
				} else {
					for(String str : result) {
						String nomeVisualizado = str.split("-")[0];
						String loginVisualizado = str.split("-")[1];
						SolicitacaoDeAmizade sol = new SolicitacaoDeAmizade(fachada, nomeVisualizado, loginVisualizado, login, idSessao, painelGeral);
						absolutePanel_1.add(sol);
					}
				}	
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}
	
	private void mostraSolicitacoesDeEmprestimo() {
		try {
			fachada.getRequisicoesDeitem(idSessao, new AsyncCallback<List<String>>() {
				@Override
				public void onSuccess(List<String> result) {
					absolutePanel_1.clear();
					if(result.isEmpty()) {
						absolutePanel_1.add(new Label("Voce nao possui nenhuma solicitacao de item!"));
					} else {
						for(String str : result) {
							String idRequisicao = str.split("-")[0];
							String beneficiado= str.split("-")[1];
							String tempoPrevisto = str.split("-")[2];
							String nomeItem = str.split("-")[3];
							SolicitacaoDeItem sol = new SolicitacaoDeItem(fachada, idSessao, idRequisicao, nomeItem, tempoPrevisto, beneficiado);
							absolutePanel_1.add(sol);
						}
					}
				}
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}
	
	private void mostraRequisicoesDeDevolucao() {
		try {
			fachada.getRequisicoesDeDevolucao(idSessao, new AsyncCallback<List<String>>() {
				@Override
				public void onSuccess(List<String> result) {
					absolutePanel_1.clear();
					if(result.isEmpty()) {
						absolutePanel_1.add(new Label("Voce nao possui nenhuma solicitacao de devolucao!"));
					} else {
						Label label = new Label("Para devolver um item, clique sobre ele.");
						absolutePanel_1.add(label);
						for(final String str : result) {
							Label requisicao = new Label(str);
							eventoDeMouseOver(requisicao);
							requisicao.addClickHandler(new ClickHandler() {
								@Override
								public void onClick(ClickEvent event) {
									devolverItem(str);
								}
							});
							absolutePanel_1.add(requisicao);
						}
					}
				}
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}
	
	private void devolverItem(String nomeItem) {
		fachada.getIdEmprestimo(idSessao, nomeItem, new AsyncCallback<Integer>() {
			@Override
			public void onSuccess(Integer result) {
				try {
					fachada.devolverItem(idSessao, result, new AsyncCallback<Void>() {
						@Override
						public void onSuccess(Void result) {
							Window.alert("Notificacao de devolucao enviada ao dono do item!");
						}
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Notificacao de devolucao enviada ao dono do item!");
						}
					});
				} catch (Exception e) {
					Window.alert(e.getMessage());
				}
			}
			@Override
			public void onFailure(Throwable caught) { 
				Window.alert(caught.getMessage());
			}
		});
	}
	
	private void eventoDeMouseOver(final Label label) {
		label.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				label.setStyleName("underlined");
			}
		});
		label.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				label.setStyleName("gwt-Label");								
			}
		});
	}
}
